package com.fone.player.activity.personal.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fone.player.R;
import com.fone.player.activity.personal.NetCollectionActivity;
import com.fone.player.cache.ImageDownloadModule;
import com.fone.player.entity.OfflineCache;
import com.fone.player.storage.StorageModule;

/**
 * 收藏界面 adapter (包括收藏界面中的开启自动下载状态都使用该adapter) ps:收藏界面使用该adapter的情况下可以
 * 通过点击图标修改下载状态
 * 
 * @author Jason_Zue
 * 
 */
public class CollectionListAdatper extends BaseAdapter {

	protected static final String TAG = "CollectionListAdatper";
	private List<OfflineCache> gridData;
	private Context mContext;

	@SuppressWarnings("unused")
	private List<Boolean> autoDownload;
	private Handler mHandler;
	@SuppressWarnings("unused")
	private int viewMode;

	/**
	 * @param mContext
	 * @param collectionData
	 *            传入条目数据List
	 * @param autoDownload
	 *            传入记录选中下载的list对象(用于修改自动下载选中后界面的改变)
	 */
	public CollectionListAdatper(Context mContext,
			List<OfflineCache> collectionData, List<Boolean> autoDownload) {
		this.mContext = mContext;
		this.gridData = collectionData;
		this.autoDownload = autoDownload;
	}

	public CollectionListAdatper(Context mContext,
			List<OfflineCache> collectionData, Handler mHandler, int viewMode) {
		this.mContext = mContext;
		this.gridData = collectionData;
		this.mHandler = mHandler;
		this.viewMode = viewMode;

	}

	@Override
	public int getCount() {
		return gridData.size();
	}

	@Override
	public Object getItem(int position) {
		return gridData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(mContext,
					R.layout.personal_collections_list_item, null);

			holder.selectedCb = (CheckBox) convertView
					.findViewById(R.id.cache_is_selected_cb);
			holder.mPlayBt = (ImageButton) convertView
					.findViewById(R.id.collection_listview_item_play_ib);
			holder.mStatueRl = (RelativeLayout) convertView
					.findViewById(R.id.collection_download_status_rl);
			holder.downloadStatusTv = (TextView) convertView
					.findViewById(R.id.collection_listview_item_download_status_tv);
			holder.seasonInfo = (TextView) convertView
					.findViewById(R.id.collection_listview_item_season_tv);
			holder.collectionType = (TextView) convertView
					.findViewById(R.id.collection_listview_item_collection_type_tv);
			holder.upDateInfo = (TextView) convertView
					.findViewById(R.id.collection_listview_item_update_info_tv);
			holder.mCacheingTv = (ImageView) convertView
					.findViewById(R.id.collection_is_in_cached);
			holder.itemImage = (ImageView) convertView
					.findViewById(R.id.collection_listview_item_pic_iv);
			holder.itemName = (TextView) convertView
					.findViewById(R.id.collection_listview_item_name_tv);
			holder.downloadProgressBar = (ProgressBar) convertView
					.findViewById(R.id.collection_listview_item_progressbar_pb);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		// 设置checkbox显示颜色
		holder.selectedCb
				.setBackgroundResource(R.drawable.collection_selected_checkbox_selector);
		holder.selectedCb.setVisibility(View.GONE);
		// 状态不可见
		holder.mStatueRl.setVisibility(View.GONE);

		if (null != gridData.get(position)) {

			holder.itemName.setText(gridData.get(position).getCacheName());// 剧集名称
			holder.seasonInfo.setText(gridData.get(position)// 剧集集数信息
					.getCacheEpisodeNum());
			holder.downloadProgressBar.setVisibility(View.GONE); // 进度条不可见
			holder.upDateInfo.setText(gridData.get(position)
					.getCacheEpisodeUpdateInfo());// 剧集更新信息
			// 设置图片
			if (!TextUtils.isEmpty(gridData.get(position).getCacheImageUrl())) {
				ImageDownloadModule.getInstance().download(
						gridData.get(position).getCacheImageUrl(),
						holder.itemImage);
			}
			holder.mPlayBt.setVisibility(View.GONE);// 可播放按钮隐藏
			
			if(gridData.get(position).getCacheIsDownlaod()){
				holder.mCacheingTv.setVisibility(View.VISIBLE);
			}else {
				holder.mCacheingTv.setVisibility(View.GONE);
			}

			// 设置剧集类型
			switch (gridData.get(position).getCacheContentType()) {
			case OfflineCache.CACHE_CONTENT_TYPE_TV:
				holder.collectionType.setText("电视剧");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_WEB_PAGE:
				holder.collectionType.setText("网页");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_MOVIE:
				holder.collectionType.setText("电影");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_ANIMATION:
				holder.collectionType.setText("动漫");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_SHORT_FILM:
				holder.collectionType.setText("短剧");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_NEWS:
				holder.collectionType.setText("新闻");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_LIVE_AUDIO:
				holder.collectionType.setText("直播音频");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_LIVE_VIDEO:
				holder.collectionType.setText("直播");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_ORIGINAL:
				holder.collectionType.setText("原创");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_VARIETY:
				holder.collectionType.setText("综艺");
				break;
			case OfflineCache.CACHE_CONTENT_TYPE_OTHER:
				holder.collectionType.setText("其他");
				break;
			default:
				holder.collectionType.setText("未知");
				break;
			}
		}

		return convertView;

	}

	class ViewHolder {
		public TextView itemName, downloadStatusTv, seasonInfo,
				 upDateInfo, collectionType;
		public ImageView itemImage, itemNew;
		public CheckBox selectedCb;
		public ProgressBar downloadProgressBar;
		public RelativeLayout mStatueRl;
		public ImageButton mPlayBt;
		public ImageView mCacheingTv;
	}

	/**
	 * 设置下载状态
	 * 
	 * @param holder
	 * @return void
	 * @throws
	 */
	@SuppressWarnings("unused")
	private void set2Download(final ViewHolder holder, int position) {
		gridData.get(position).setCacheDownloadState(
				OfflineCache.CACHE_STATE_DOWNLOADING);
		changeStatus(holder, R.drawable.collect_icon_download,
				R.color.cache_item_status_text_color, "操作中..", false);
		StorageModule.getInstance().startCache(gridData.get(position));
	}

	/**
	 * 设置暂停状态
	 * 
	 * @param holder
	 * @return void
	 * @throws
	 */
	@SuppressWarnings("unused")
	private void set2Pause(final ViewHolder holder, int position) {
		gridData.get(position).setCacheDownloadState(
				OfflineCache.CACHE_STATE_PAUSE);
		changeStatus(holder, R.drawable.collect_icon_pause,
				R.color.cache_item_status_text_color, "操作中..", false);
		StorageModule.getInstance().pauseCache(gridData.get(position));
	}

	/**
	 * 修改下载状态
	 * 
	 * @param holder
	 * @param drawableId
	 *            图片R文件
	 * @param textColor
	 *            下载文字颜色R文件
	 * @param statusText
	 *            下载文字内容 String格式
	 * @param isShow
	 *            是否显示图标
	 * @return void
	 * @throws
	 */
	private void changeStatus(final ViewHolder holder, int drawableId,
			int textColor, String statusText, boolean isShow) {
		Drawable drawable = mContext.getResources().getDrawable(drawableId);

		holder.downloadStatusTv.setText(statusText);
		drawable.setBounds(0, 0, drawable.getMinimumWidth(),
				drawable.getMinimumHeight());
		if (isShow) {
			holder.downloadStatusTv.setCompoundDrawables(null, drawable, null,
					null);
		} else {
			holder.downloadStatusTv
					.setCompoundDrawables(null, null, null, null);
		}
		holder.downloadStatusTv.setTextColor(mContext.getResources().getColor(
				textColor));
	}

	/**
	 * 添加条目到缓存列表
	 * 
	 * @param position
	 * @param isChecked
	 * @return void
	 * @throws
	 */
	@SuppressWarnings("unused")
	private void addItem2Cache(final int position, boolean isChecked,
			int viewMode) {
		OfflineCache offlineCache = new OfflineCache();
		offlineCache.setCacheCID(gridData.get(position).getCacheCID());
		offlineCache.setCacheCCID(gridData.get(position).getCacheCCID());
		offlineCache.setCacheCLID(gridData.get(position).getCacheCLID());
		offlineCache.setCacheVideoId(gridData.get(position).getCacheVideoId());
		offlineCache.setCacheContentType(gridData.get(position)
				.getCacheContentType());
		offlineCache.setCacheHierarchyType(OfflineCache.CACHE_HIERARCHY_FILE);
		offlineCache
				.setCacheImageUrl(gridData.get(position).getCacheImageUrl());
		offlineCache.setCacheIsDownlaod(isChecked);
		offlineCache.setCacheName(gridData.get(position).getCacheName());
		offlineCache.setCacheXYZPlayUrl(gridData.get(position)
				.getCacheXYZPlayUrl());
		offlineCache.setCacheIsDownloadable(gridData.get(position)
				.getCacheIsDownloadable());
		offlineCache
				.setCacheDramaUrl(gridData.get(position).getCacheDramaUrl());
		offlineCache.setCacheEpisodeNum(gridData.get(position)
				.getCacheEpisodeNum());
		offlineCache.setCacheEpisodeUpdateInfo(gridData.get(position)
				.getCacheEpisodeUpdateInfo());
		offlineCache.setCacheIsVip(gridData.get(position).getCacheIsVip());
		// 解决复用问题(position完全用offlineCache的记录)
		if (gridData.get(position).getCacheIsDownlaod() == true) {
			// FoneUtil.showToast(mContext, "添加到缓存列表");
			Message message = mHandler
					.obtainMessage(NetCollectionActivity.ADD_TO_CACHE);
			message.obj = offlineCache;
			message.arg1 = position;
			message.arg2 = viewMode;
			mHandler.sendMessage(message);
		} else {
			// FoneUtil.showToast(mContext, "删除缓存列表对应数据");
			Message message = mHandler
					.obtainMessage(NetCollectionActivity.DELETE_FROM_CACHE);
			message.obj = offlineCache;
			message.arg1 = position;
			message.arg2 = viewMode;
			mHandler.sendMessage(message);
		}

	}

}
